package com.project.imageinterference.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 调用远程接口的服务类
 * @Author cwhao
 * @Date 2025/2/6 下午2:04
 */
@Service
public class RemoteService {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 调用远程接口
     *
     * @param url        远程接口地址
     * @param imageArray 三维数组数据
     * @return 远程接口返回的结果
     */
    public String callRemoteApi(String url, int[][][] imageArray) {
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("image", imageArray);

        // 发送HTTP请求
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

        // 返回远程接口的响应
        return response.getBody();
    }
}
